import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-10
 * Time: 16:54
 */
public class Test50 {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode() {

        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(p == root || q == root) {
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        if(leftTree != null && rightTree != null) {
            return root;
        }
        if(leftTree != null) {
            return leftTree;
        }
        if(rightTree != null) {
            return rightTree;
        }
        return null;
    }

    public static TreeNode build(String s) {
        String input = s.replace("[", "").replace("]", "");
        String[] parts = input.split(",");
        Integer[] num = new Integer[parts.length];

        for (int i = 0; i < parts.length; i++) {
            if (!parts[i].equals("null")) {
                num[i] = Integer.parseInt(parts[i]);
            } else {
                num[i] = null;
            }
        }

        // 重点
        if (num.length == 0 || num[0] == null) {
            return null;
        }
        TreeNode root = new TreeNode(num[0]);
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int index = 1;

        while (!q.isEmpty() && index < num.length) {
            TreeNode cur = q.poll();

            if (index < num.length && num[index] != null) {
                cur.left = new TreeNode(num[index]);
                q.offer(cur.left);
            }
            index++;

            if (index < num.length && num[index] != null) {
                cur.right = new TreeNode(num[index]);
                q.offer(cur.right);
            }
            index++;
        }
        return root;
    }

    public static TreeNode findNode(TreeNode root, int target) {
        if (root == null) return null;
        if (root.val == target) return root;

        TreeNode left = findNode(root.left, target);
        if (left != null) return left;

        return findNode(root.right, target);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        TreeNode root = build(s);
        int m = sc.nextInt();
        int n = sc.nextInt();
        TreeNode p = findNode(root,m);
        TreeNode q = findNode(root,n);
        TreeNode ret = lowestCommonAncestor(root,p,q);
        System.out.println(ret.val);
    }
}
